﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using BarStructures.Deformed;

namespace BarStructures
{
    public class Node: IEquatable<Node>
    {
        public double X { get; private set; }

        public double Y { get; private set; }

        public int UniqueIndex { get; private set; }

        public int SystemIndex { get; internal set; }

        public Support Support { get; internal set; }

        public DegreeSelection DegreeSelection { get; internal set; }

        #region Constructors

        internal Node(double x, double y, int uniqueIndex)
        {
            this.X = x;
            this.Y = y;
            this.UniqueIndex = uniqueIndex;
            this.DegreeSelection = new DegreeSelection();
        }

        internal Node(double x, double y, int uniqueIndex, int systemIndex)
            : this(x, y, uniqueIndex)
        {
            this.SystemIndex = systemIndex;
        }

        #endregion

        public void SelectDegrees(bool x, bool y, bool r)
        {
            this.DegreeSelection.X = x;
            this.DegreeSelection.Y = y;
            this.DegreeSelection.Rot = r;
        }

        public void CreateSupport(bool sx, bool sy, bool sr)
        {
            Support support = new Support(sx, sy, sr);
            this.Support = support;
        }

        #region IEquatable<Node> Members

        public bool Equals(Node other)
        {
            return this.UniqueIndex == other.UniqueIndex;
        }

        #endregion

        public void Draw(IDrawBoard d)
        {
            int w = 4;
            int w2 = w * 2;
            PointF orign = d.GetWindowCoordinates(this.X, this.Y);
            Graphics g = d.GetGraphics();
            g.DrawRectangle(Pens.Green, orign.X - w, orign.Y - w, w2, w2);
            Font f = new Font(FontFamily.GenericSansSerif, 7);
            g.DrawString(this.SystemIndex.ToString(), f, Brushes.Teal, orign.X + 8, orign.Y - 15);

            //рисование основных степеней свободы
            if (this.DegreeSelection.X)
                g.DrawLine(Pens.Orange, orign.X + w, orign.Y - w, orign.X + w, orign.Y + w);
            if (this.DegreeSelection.Y)
                g.DrawLine(Pens.Orange, orign.X - w, orign.Y + w, orign.X + w, orign.Y + w);
            if (this.DegreeSelection.Rot)
                g.DrawLine(Pens.Orange, orign.X - w, orign.Y + w, orign.X + w, orign.Y - w);

            //рисование закреплений
            if (this.Support.Sx)
                if (this.DegreeSelection.X)
                    g.DrawLine(Pens.Red, orign.X + w, orign.Y - w, orign.X + w, orign.Y + w);
                else
                    g.DrawLine(Pens.Violet, orign.X + w, orign.Y - w, orign.X + w, orign.Y + w);
            if (this.Support.Sy)
                if (this.DegreeSelection.Y)
                    g.DrawLine(Pens.Red, orign.X - w, orign.Y + w, orign.X + w, orign.Y + w);
                else
                    g.DrawLine(Pens.Violet, orign.X - w, orign.Y + w, orign.X + w, orign.Y + w);
            if (this.Support.Sr)
                if (this.DegreeSelection.Rot)
                    g.DrawLine(Pens.Red, orign.X - w, orign.Y + w, orign.X + w, orign.Y - w);
                else
                    g.DrawLine(Pens.Violet, orign.X - w, orign.Y + w, orign.X + w, orign.Y - w);
        }

        public void Draw(IDrawBoard d, NodeDisplacement displacemnt, double scale)
        {
            PointF orign = d.GetWindowCoordinates(this.X + displacemnt.Dx * scale, this.Y + displacemnt.Dy * scale);
            Graphics g = d.GetGraphics();
            g.TranslateTransform(orign.X, orign.Y);
            g.RotateTransform((float)(180 * displacemnt.Da / Math.PI));
            g.DrawRectangle(Pens.Red, -4, -4, 8, 8);
            g.ResetTransform();
        }
    }
}
